using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public enum MapItemType
{
    Fire,
    Fight,
    Space,
    Shop,
    Boss
}

public class MapItem : Button3D
{
    public int id;
    public bool canMouseDown; // 是否能点击
    public MapItemType type;
    public List<MapItem> nextMapItem;

    [SerializeField] Animator fight_ac;
    [SerializeField] Animator fire_ac;
    [SerializeField] Animator space_ac;

    /// <summary>
    /// 初始化节点
    /// </summary>
    public void Setup(int id, MapItemType type, List<MapItem> nextItem)
    {
        this.id = id;
        this.type = type;
        this.canMouseDown = false;
        this.nextMapItem = nextItem;

        UpdateVisual();
        onClick.RemoveAllListeners();

        onClick.AddListener(() =>
        {
            if (!canMouseDown) return;

            // 发送 GA
            EnterGameGA ga = new EnterGameGA(id);
            ActionSystem.Instance.AddReaction(ga);

            // 延迟到下一帧再处理场景切换和刷新，保证 POST 回调执行
            StartCoroutine(HandleNodeClick());
        });
    }

    private IEnumerator HandleNodeClick()
    {
        // 等一帧，保证 ActionSystem 的 POST 执行
        yield return null;

        // 更新 InfoSystem 当前节点
        InfoSystem.Instance.nowMapItemId = id;
        InfoSystem.Instance.SaveGame();

        // 刷新 MapView 英雄和可点击节点
        MapView mapView = Object.FindFirstObjectByType<MapView>();
        if (mapView != null)
        {
            mapView.UpdateClickableNodes();
        }

        // 场景切换
        switch (type)
        {
            case MapItemType.Fight:
                SceneManager.sceneLoaded += OnFightSceneLoaded;
                SceneManager.LoadSceneAsync("FightScene");
                break;
            case MapItemType.Fire:
                SceneManager.LoadSceneAsync("FireScene");
                break;
            case MapItemType.Space:
                Debug.Log($"进入空地，id={id}");
                break;
            case MapItemType.Shop:
                Debug.Log($"进入商店，id={id}");
                break;
            case MapItemType.Boss:
                Debug.Log($"进入Boss战，id={id}");
                break;
        }
    }

    private void OnFightSceneLoaded(Scene scene, LoadSceneMode mode)
    {
        if (scene.name == "FightScene")
        {
            InitSystem.Instance.SetupById(this.id);
            SceneManager.sceneLoaded -= OnFightSceneLoaded;
        }
    }

    /// <summary>
    /// 更新节点显示
    /// </summary>
    private void UpdateVisual()
    {
        if (fight_ac != null) fight_ac.gameObject.SetActive(false);
        if (fire_ac != null) fire_ac.gameObject.SetActive(false);
        if (space_ac != null) space_ac.gameObject.SetActive(false);

        switch (type)
        {
            case MapItemType.Fight:
                if (fight_ac != null) fight_ac.gameObject.SetActive(true);
                break;
            case MapItemType.Fire:
                if (fire_ac != null) fire_ac.gameObject.SetActive(true);
                break;
            case MapItemType.Space:
                if (space_ac != null) space_ac.gameObject.SetActive(true);
                break;
        }
    }
}
